package com.ia.aistream.controller.dxfcomponent;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.ia.aistream.common.annotation.Log;
import com.ia.aistream.manager.dxfcomponent.conf.DrawMapConf;
import com.ia.aistream.model.dxfcomponent.entity.DxfComponent;
import com.ia.aistream.service.dxfcomponent.IDxfComponentService;
import com.ia.aistream.service.dxfcomponent.impl.DxfDrawServiceImpl;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
///import com.ia.aistream.common.annotation;
import com.ia.aistream.common.enums.BusinessType;
import com.ia.aistream.model.dxfcomponent.entity.DxfDraw;
import com.ia.aistream.service.dxfcomponent.IDxfDrawService;
import com.ia.aistream.common.core.controller.BaseController;
import com.ia.aistream.common.core.domain.AjaxResult;
import com.ia.aistream.common.utils.poi.ExcelUtil;
import com.ia.aistream.common.core.page.TableDataInfo;

/**
 * dxf文件索引信息Controller
 * 
 * @author ymj
 * @date 2020-12-04
 */
@Controller
@RequestMapping("/dxfcomponent/dxf")
public class DxfDrawController extends BaseController
{
    private String prefix = "dxfcomponent/dxf";

    @Autowired
    private IDxfDrawService dxfDrawService;
    @Autowired
    private IDxfComponentService componentService;

    @RequiresPermissions("dxfcomponent:dxf:view")
    @GetMapping()
    public String dxf()
    {
        return prefix + "/dxf";
    }

    /**
     * 查询dxf文件索引信息列表
     */
//    @RequiresPermissions("dxfcomponent:dxf:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DxfDraw dxfDraw)
    {
        startPage();
        List<DxfDraw> list = dxfDrawService.selectDxfDrawList(dxfDraw);
        if(list==null){
            list = new ArrayList<>();
        }
        return getDataTable(list);
    }

    /**
     * 导出dxf文件索引信息列表
     */
//    @RequiresPermissions("dxfcomponent:dxf:export")
    @Log(title = "dxf文件索引信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DxfDraw dxfDraw)
    {
        List<DxfDraw> list = dxfDrawService.selectDxfDrawList(dxfDraw);
        ExcelUtil<DxfDraw> util = new ExcelUtil<DxfDraw>(DxfDraw.class);
        return util.exportExcel(list, "dxf");
    }

    /**
     * 新增dxf文件索引信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存dxf文件索引信息
     */
//    @RequiresPermissions("dxfcomponent:dxf:add")
    @Log(title = "dxf文件索引信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DxfDraw dxfDraw)
    {
        return toAjax(dxfDrawService.insertDxfDraw(dxfDraw));
    }

    /**
     * 修改dxf文件索引信息
     */
    @GetMapping("/edit/{drawId}")
    public String edit(@PathVariable("drawId") Long drawId, ModelMap mmap)
    {
        DxfDraw dxfDraw = dxfDrawService.selectDxfDrawById(drawId);
        mmap.put("dxfDraw", dxfDraw);
        return prefix + "/edit";
    }

    /**
     * 修改保存dxf文件索引信息
     */
//    @RequiresPermissions("dxfcomponent:dxf:edit")
    @Log(title = "dxf文件索引信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DxfDraw dxfDraw)
    {
        return toAjax(dxfDrawService.updateDxfDraw(dxfDraw));
    }

    /**
     * 删除dxf文件索引信息
     */
//    @RequiresPermissions("dxfcomponent:dxf:remove")
    @Log(title = "dxf文件索引信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(dxfDrawService.deleteDxfDrawByIds(ids));
    }

    /***
     * 实现索引图纸数据的查看
     * @param drawId
     * @param mmap
     * @return
     */
    @GetMapping("/search")
    public String searchDraw(Long drawId, ModelMap mmap)
    {
        Map<String, Object> map = dxfDrawService.getDxfDrawService(drawId);

        mmap.addAttribute("images", map.get(DrawMapConf.DXF_IMAGES.value));
//        // 获取索引信息
        mmap.addAttribute("dwgIndex",map.get(DrawMapConf.DWG_DXF_INDEX.value));
        return prefix + "/view";
    }

    /***
     * 实现图纸的查看
     * @param drawId
     * @param mmap
     * @return
     */
    @GetMapping("/view")
    public String viewImg(Long drawId, ModelMap mmap){
        Map<String, Object> map = dxfDrawService.getDxfDrawService(drawId);
        mmap.addAttribute("images", map.get(DrawMapConf.DXF_IMAGES.value));
        return prefix + "/dxfImg";
    }

    /***
     * 实现设备信息的查询
     * @param drawId
     * @param mmap
     * @return
     */
    @ResponseBody
    @PostMapping("/component/{drawId}")
    public TableDataInfo getComponent(@PathVariable("drawId") Long drawId, ModelMap mmap){
        DxfComponent dxfComponent = new DxfComponent();
        dxfComponent.setDrawId(drawId);
        return getDataTable(componentService.selectDxfComponentList(dxfComponent));
    }
}
